﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Csla;
using Csla.Data;
using Csla.Security;
using Kiosk.Library;

namespace Kiosk.WebManagement
{
    public partial class Notification : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                pnlAdd.Visible = false;
            }
        }

        private Guid GetLocationGuid(string locationName)
        {
            foreach (LocationInfo aLocation in LocationReadOnlyList.GetAll())
            {
                if (aLocation.Name == locationName)
                {
                    return aLocation.LocationID;
                }
            }

            return Guid.Empty;
        }

        private Guid GetNotificationTypeGuid(string notificationTypeName)
        {
            foreach (NotificationTypeInfo aNotificationType in NotificationTypeReadOnlyList.GetAll())
            {
                if (aNotificationType.Name == notificationTypeName)
                {
                    return aNotificationType.NotificationTypeID;
                }
            }

            return Guid.Empty;
        }

        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static string[] GetLocationList(string prefixText, int count)
        {
            var locations = from loc in LocationReadOnlyList.GetAllStartingWith(prefixText)
                            orderby loc.Name
                            select loc.Name;

            return locations.ToArray();
        }

        #region Validation Events
        protected void cvDate_ServerValidate(object source, ServerValidateEventArgs args)
        {
            // Verify that the date is in the future
            if (DateTime.Parse(args.Value) < DateTime.Today)
            {
                cvDate.ErrorMessage = "Date cannot be in the past.";

                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }

        protected void cvLocation_ServerValidate(object source, ServerValidateEventArgs args)
        {
            // Verify that the location exists.
            if(Location.Exists(GetLocationGuid(txtLocation.Text)))
            {
                args.IsValid = true;
            }
            else
            {
                cvLocation.ErrorMessage = "Location is invalid.";

                args.IsValid = false;
            }
        }

        protected void valUserFound_ServerValidate(object source, ServerValidateEventArgs args)
        {
            EmployeeInfo emp = EmployeeInfo.GetEmployeeInfoByUserName(User.Identity.Name);
            if (emp == null)
            {
                args.IsValid = false;
            }
            else
                args.IsValid = true;


        }
        #endregion

        #region Other Control Events
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Kiosk.Library.Notification myNotification = (Kiosk.Library.Notification)Session["note"];

                // Create the new notification
                myNotification.NotificationDate = DateTime.Parse(txtDate.Text + " " + txtTime.Text);

                EmployeeInfo emp = EmployeeInfo.GetEmployeeInfoByUserName(User.Identity.Name);
                myNotification.EmployeeID = emp.EmployeeID;
                myNotification.Title = txtTitle.Text;
                myNotification.Description = txtDescription.Text;
                myNotification.LocationID = GetLocationGuid(txtLocation.Text);
                myNotification.NotificationTypeID = GetNotificationTypeGuid(ddlNotificationType.SelectedItem.Text);

                myNotification.Save();
                pnlAdd.Visible = false;
                gvNotificationList.DataBind();

            }
        }
        #endregion

        // Jordan smells
        protected void CSLADataSource1_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
        {
            e.BusinessObject = NotificationReadOnlyList.GetAll();
        }

        protected void CSLADataSource1_DeleteObject(object sender, Csla.Web.DeleteObjectArgs e)
        {
            Guid notificationID = new Guid(e.Keys["NotificationID"].ToString());

            Kiosk.Library.Notification.DeleteNotification(notificationID);

            e.RowsAffected = 1;
        }

        protected void gvNotificationList_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlAdd.Visible = true;
            Session["note"] = Library.Notification.GetNotification((Guid)gvNotificationList.SelectedDataKey.Value);
            BindNotification();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            pnlAdd.Visible = true;
            Session["note"] = Library.Notification.NewNotification();
            ((Kiosk.Library.Notification)Session["note"]).NotificationDate = DateTime.Now;
            BindNotification();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Session["note"] = null;
            pnlAdd.Visible = false;
            BindNotification();
        }

        private void BindNotification()
        {
            Library.Notification note = (Library.Notification)Session["note"]; 
            if (note == null || note.IsNew)
            {
                txtTime.Text = "";
                txtTitle.Text = "";
                txtDescription.Text = "";
                txtLocation.Text = "";
                ddlNotificationType.SelectedIndex = -1;
                txtLocation.Text = "";
                //if (note != null && note.IsNew)
                //{
                //    txtLocation.Text = LocationInfo.GetLocationInfoById(
                //        EmployeeInfo.GetEmployeeInfoByUserName(User.Identity.Name).LocationID).Name;
                //}

            
            }
            else
            {
                txtDate.Text = note.NotificationDate.Value.ToShortDateString();
                txtTime.Text = note.NotificationDate.Value.ToShortTimeString();
                txtTitle.Text =  note.Title;
                txtDescription.Text = note.Description;
                txtLocation.Text = LocationInfo.GetLocationInfoById(note.LocationID).Name;

                ddlNotificationType.SelectedValue = note.NotificationTypeID.ToString();
            }
        }



    }
}
